package com.cdivtc.management.controller;

import com.cdivtc.management.dao.StuTaskStatusMapper;
import com.cdivtc.management.pojo.Result;
import com.cdivtc.management.pojo.StuTaskStatus;
import com.cdivtc.management.pojo.Student;
import com.cdivtc.management.pojo.StudentsDoTasks;
import com.cdivtc.management.service.StudentsDoTasksService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/studentsDoTasks")
public class StudentsDoTasksController {
    @Autowired
    private StudentsDoTasksService studentsDoTasksService;

    @Autowired
    private StuTaskStatusMapper stuTaskStatusMapper;

    @RequestMapping("updStuTask")
    public Result updStuTask(String id, String teaDesc, String flag, String taskId, String stuId) {
        if (id != null) {
            StudentsDoTasks studentsDoTasks = studentsDoTasksService.updStuTask(id, teaDesc, flag, taskId, stuId);
            if (studentsDoTasks != null)
                return new Result().ok().addData(studentsDoTasks);
            else
                return new Result().error("查询失败！！！");
        } else {
            return new Result().error("查询失败！！！");
        }
    }

    @RequestMapping("selStuTaskByTeaId")
    public Result selStuTaskByTeaId(String id) {
        System.out.println("StudentsDoTasksController line 21= " + id);
        if (id != null) {
            List<Map<String, Object>> list = studentsDoTasksService.selStuTaskByTeaId(id);
            if (list != null)
                return new Result().ok().addData(list);
            else
                return new Result().error("查询失败！！！");
        } else {
            return new Result().error("查询失败！！！");
        }
    }

    @RequestMapping("selStuTaskById")
    public Result selStuTaskById(String id) {
        System.out.println("StudentsDoTasksController line 21= " + id);
        if (id != null) {
            StudentsDoTasks studentsDoTasks = studentsDoTasksService.selStuTaskById(id);
            if (studentsDoTasks != null)
                return new Result().ok().addData(studentsDoTasks);
            else
                return new Result().error("查询失败！！！");
        } else {
            return new Result().error("查询失败！！！");
        }
    }

    @RequestMapping("selStuTask")
    public Result selStuTask(String stuId, String taskId) {
        System.out.println("StudentsDoTasksController line 21= " + stuId);
        System.out.println("StudentsDoTasksController line 22= " + taskId);
        if (stuId != null && taskId != null) {
            List<Map<String, Object>> list = studentsDoTasksService.selStuTask(stuId, taskId);
            if (list != null)
                return new Result().ok().addData(list);
            else
                return new Result().error("查询失败！！！");
        } else {
            return new Result().error("查询失败！！！");
        }
    }

    @RequestMapping("selStuTaskStatus")
    public Result selStuTaskStatus(String stuId, String taskId) {
        System.out.println("StudentsDoTasksController line 83= " + stuId);
        if (stuId != null && taskId != null) {
            Example example = new Example(StuTaskStatus.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("stuId", stuId);
            criteria.andEqualTo("taskId", taskId);
            StuTaskStatus stuTaskStatus = stuTaskStatusMapper.selectOneByExample(example);
            if (stuTaskStatus != null)
                return new Result().ok().addData(stuTaskStatus);
            else
                return new Result().error("查询失败！！！");
        } else {
            return new Result().error("查询失败！！！");
        }
    }

    @RequestMapping("stuAddTask")
    public Result stuAddTask(AddBean addBean) {
        System.out.println("StudentsDoTasksController line 22= " + addBean);
        if (addBean.getStuId() != null) {
            StudentsDoTasks studentsDoTasks = studentsDoTasksService.addStuTask(addBean);
            if (studentsDoTasks != null)
                return new Result().ok().addData(studentsDoTasks);
            else
                return new Result().error("添加失败！！！");
        } else {
            return new Result().error("添加失败！！！");
        }
    }

    @Data
    public class AddBean {
        private String stuId;
        private String teaId;
        private String taskId;
        private String stuRemarks;
        private List<String> stuUpFile;
    }

}
